package com.liu.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.liu.dao1",
entityManagerFactoryRef = "entityManagerFactoryBeanOne",
transactionManagerRef = "platformTransactionManagerOne")
public class JpaConfigOne {
    @Resource(name="dsOne")
    DataSource dsOne;
    @Autowired
    JpaProperties jpaProperties;

     @Bean
    @Primary
         //创建entityManageFactoryBeanOne 工厂实例,用来加载 EntityManageerFactoryBuilder对象
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBeanOne (EntityManagerFactoryBuilder builder){
            return builder.dataSource(dsOne)
                    .properties(jpaProperties.getProperties())
                    .packages("com.liu.model")
                    .persistenceUnit("pu1")
                    .build();
    }
    @Bean
    PlatformTransactionManager platformTransactionManagerOne(EntityManagerFactoryBuilder builder){
         LocalContainerEntityManagerFactoryBean factoryBean =
                 entityManagerFactoryBeanOne(builder);//上一个@Bean注解的方法创造的实体管理工厂
         return new JpaTransactionManager(factoryBean.getObject());
    }






}













